package com.zq.learn.spark.springspark;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * SparkConfig
 * Spark上下文环境配置
 *
 * @author ZhangQi
 * @date 2020/2/12
 */
@Configuration
public class SparkConfig {
  private String master="local[2]";
  private String appName="orderStatistic";

  /**
   * Spark配置
   * @return
   * @throws Exception
   */
  @Bean
  @ConditionalOnMissingBean(SparkConf.class)
  public SparkConf sparkConf() throws Exception {
    SparkConf conf = new SparkConf().setAppName(appName).setMaster(master);
    return conf;
  }

  /**
   * Spark上下文
   * @return
   * @throws Exception
   */
  @Bean
  @ConditionalOnMissingBean(JavaSparkContext.class)
  public JavaSparkContext sparkContext() throws Exception {
    return new JavaSparkContext(sparkConf());
  }

}
